package com.abel.mvc.service.impl;

import com.abel.mvc.entity.Mail;
import com.abel.mvc.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

/**
 * @author abel.he
 * @data 2018/8/11 11:39
 */
@Service("messageService")
public class MessageServiceImpl implements MessageService {

    @Autowired
    SimpleMailMessage simpleMailMessage;

    @Autowired
    JavaMailSenderImpl javaMailSender;

    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public void sendMail(final Mail mail) {
        this.threadPoolTaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                simpleMailMessage.setFrom(mail.getFrom());
                simpleMailMessage.setTo(mail.getTo());
                simpleMailMessage.setSubject(mail.getSubject());
                simpleMailMessage.setText(mail.getContent());
                javaMailSender.send(simpleMailMessage);
            }
        });
    }
}
